﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Frontend.Grids;
using SpeakupBackend;
using SpeakupBackend.Themes;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Frontend
{
    class BusinessThemeGrid : HighlightableGrid
    {
        public BusinessThemeGrid()
        {
            HighlightBrush = Brushes.LightPink;

            Color color = Colors.LightGreen;
            CurrentBrush = new SolidColorBrush(Color.FromArgb(100, color.R, color.G, color.B));
        }

        BackendContext m_backendCtx;
        public BackendContext BackendCtx
        {
            get
            {
                return m_backendCtx;
            }
            set
            {
                if (m_backendCtx != value)
                {
                    m_backendCtx = value;
                }
            }
        }

        List<Week> m_weeks;
        List<Week> Weeks
        {
            get
            {
                if (m_weeks == null)
                {
                    m_weeks = new List<Week>(Week.GetWeeks());
                }

                return m_weeks;
            }
        }

        List<BusinessLevelGroup> m_levelGroups;
        List<BusinessLevelGroup> LevelGroups
        {
            get
            {
                if (m_levelGroups == null)
                {
                    m_levelGroups = new List<BusinessLevelGroup>(BusinessLevelGroup.BusinessFactory.GetLevelGroups());
                }
                return m_levelGroups;
            }
        }

        int IndexOf(Week week)
        {
            return Weeks.IndexOf(week) + 1;
        }

        int IndexOf(BusinessLevelGroup levelGroup)
        {
            return LevelGroups.IndexOf(levelGroup) + 1;
        }

        UIElementFactory UIElementFactory = new UIElementFactory();

        public void SetupGrid()
        {
            Children.Clear();

            SetupRowHeaders();
            SetupColumnHeaders();
            SetupData();

            int level = BackendCtx.Stats.Level;
            BusinessLevelGroupFactory factory = BusinessLevelGroup.BusinessFactory;

            BusinessLevelGroup levelGroup  = factory.GetLevelGroups().First();

            if (factory.IsValidLevel(level))
            {
                levelGroup = factory.GetLevelGroup(BackendCtx.Stats.Level);
            }
            
            CurrentColumn = IndexOf(levelGroup);
        }

        void SetupRowHeaders()
        {
            RowDefinitions.Clear();

            for (int i = 0; i < Weeks.Count + 1; i++)
            {
                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = new GridLength(1, GridUnitType.Star);
                RowDefinitions.Add(rowdef);
            }

            foreach (Week w in Weeks)
            {
                UIElement element = UIElementFactory.CreateWeek(w);
                Grid.SetRow(element, IndexOf(w));
                Grid.SetColumn(element, 0);

                Children.Add(element);
            }
        }

        void SetupColumnHeaders()
        {
            ColumnDefinitions.Clear();

            ColumnDefinition coldef = new ColumnDefinition();
            coldef.Width = GridLength.Auto;
            ColumnDefinitions.Add(coldef);

            for (int i = 1; i < LevelGroups.Count + 1; i++)
            {
                coldef = new ColumnDefinition();
                coldef.Width = new GridLength(1, GridUnitType.Star);
                ColumnDefinitions.Add(coldef);
            }

            foreach (BusinessLevelGroup level in LevelGroups)
            {
                UIElement element = UIElementFactory.CreateLevelGroup(level);
                Grid.SetColumn(element, IndexOf(level));
                Grid.SetRow(element, 0);
                Children.Add(element);
            }
        }

        void SetupData()
        {
            foreach (BusinessTheme theme in BackendCtx.BusinessThemes)
            {
                UIElement element = UIElementFactory.CreateBusinessTheme(theme);
                Grid.SetRow(element, IndexOf(theme.Week));
                Grid.SetColumn(element, IndexOf(theme.LevelGroup));
                Children.Add(element);
            }
        }        
    }

}
